import { defineStore } from "pinia";

import type { categoryListType } from "@/api/category";
import {
  reqCategort1List,
  reqCategort2List,
  reqCategort3List,
} from "@/api/category";

//当前state的类型
export interface categoryStateType {
  category1List: categoryListType;
  category2List: categoryListType;
  category3List: categoryListType;
  category1Id: number | null;
  category2Id: number | null;
  category3Id: number | null;
}

//定义category的store
const useCategoryStore = defineStore("category", {
  state: (): categoryStateType => {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      //收集1级分类的选择
      category1Id: null,
      //收集2级分类的选择
      category2Id: null,
      //收集3级分类的选择
      category3Id: null,
    };
  },
  actions: {
    //请求category1List的方法
    async getCategory1List() {
      const result = await reqCategort1List();
      this.category1List = result;
    },

    //请求category2List的方法
    async getCategory2List() {
      const result = await reqCategort2List(this.category1Id as number);
      this.category2List = result;
    },

    //请求category3List的方法
    async getCategory3List() {
      const result = await reqCategort3List(this.category2Id as number);
      this.category3List = result;
    },
  },
});

export default useCategoryStore;
